package com.room.search.web.rest;

import com.room.search.repository.RoomGoodSearchRepository;
import com.room.search.service.RoomGoodSearchService;
import com.room.search.service.dto.RoomGoodSearchDTO;
import com.room.search.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.room.search.domain.RoomGoodSearch}.
 */
@RestController
@RequestMapping("/api")
public class RoomGoodSearchResource {

    private final Logger log = LoggerFactory.getLogger(RoomGoodSearchResource.class);

    private static final String ENTITY_NAME = "roomGoodSearch";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final RoomGoodSearchService roomGoodSearchService;

    private final RoomGoodSearchRepository roomGoodSearchRepository;

    public RoomGoodSearchResource(RoomGoodSearchService roomGoodSearchService, RoomGoodSearchRepository roomGoodSearchRepository) {
        this.roomGoodSearchService = roomGoodSearchService;
        this.roomGoodSearchRepository = roomGoodSearchRepository;
    }

    /**
     * {@code POST  /room-good-searches} : Create a new roomGoodSearch.
     *
     * @param roomGoodSearchDTO the roomGoodSearchDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new roomGoodSearchDTO, or with status {@code 400 (Bad Request)} if the roomGoodSearch has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/room-good-searches")
    public ResponseEntity<RoomGoodSearchDTO> createRoomGoodSearch(@Valid @RequestBody RoomGoodSearchDTO roomGoodSearchDTO)
        throws URISyntaxException {
        log.debug("REST request to save RoomGoodSearch : {}", roomGoodSearchDTO);
        if (roomGoodSearchDTO.getId() != null) {
            throw new BadRequestAlertException("A new roomGoodSearch cannot already have an ID", ENTITY_NAME, "idexists");
        }
        RoomGoodSearchDTO result = roomGoodSearchService.save(roomGoodSearchDTO);
        return ResponseEntity
            .created(new URI("/api/room-good-searches/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /room-good-searches/:id} : Updates an existing roomGoodSearch.
     *
     * @param id the id of the roomGoodSearchDTO to save.
     * @param roomGoodSearchDTO the roomGoodSearchDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomGoodSearchDTO,
     * or with status {@code 400 (Bad Request)} if the roomGoodSearchDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the roomGoodSearchDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/room-good-searches/{id}")
    public ResponseEntity<RoomGoodSearchDTO> updateRoomGoodSearch(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody RoomGoodSearchDTO roomGoodSearchDTO
    ) throws URISyntaxException {
        log.debug("REST request to update RoomGoodSearch : {}, {}", id, roomGoodSearchDTO);
        if (roomGoodSearchDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomGoodSearchDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomGoodSearchRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        RoomGoodSearchDTO result = roomGoodSearchService.save(roomGoodSearchDTO);
        return ResponseEntity
            .ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomGoodSearchDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code PATCH  /room-good-searches/:id} : Partial updates given fields of an existing roomGoodSearch, field will ignore if it is null
     *
     * @param id the id of the roomGoodSearchDTO to save.
     * @param roomGoodSearchDTO the roomGoodSearchDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomGoodSearchDTO,
     * or with status {@code 400 (Bad Request)} if the roomGoodSearchDTO is not valid,
     * or with status {@code 404 (Not Found)} if the roomGoodSearchDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the roomGoodSearchDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/room-good-searches/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<RoomGoodSearchDTO> partialUpdateRoomGoodSearch(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody RoomGoodSearchDTO roomGoodSearchDTO
    ) throws URISyntaxException {
        log.debug("REST request to partial update RoomGoodSearch partially : {}, {}", id, roomGoodSearchDTO);
        if (roomGoodSearchDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomGoodSearchDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomGoodSearchRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<RoomGoodSearchDTO> result = roomGoodSearchService.partialUpdate(roomGoodSearchDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomGoodSearchDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /room-good-searches} : get all the roomGoodSearches.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of roomGoodSearches in body.
     */
    @GetMapping("/room-good-searches")
    public List<RoomGoodSearchDTO> getAllRoomGoodSearches() {
        log.debug("REST request to get all RoomGoodSearches");
        return roomGoodSearchService.findAll();
    }

    /**
     * {@code GET  /room-good-searches/:id} : get the "id" roomGoodSearch.
     *
     * @param id the id of the roomGoodSearchDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the roomGoodSearchDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/room-good-searches/{id}")
    public ResponseEntity<RoomGoodSearchDTO> getRoomGoodSearch(@PathVariable Long id) {
        log.debug("REST request to get RoomGoodSearch : {}", id);
        Optional<RoomGoodSearchDTO> roomGoodSearchDTO = roomGoodSearchService.findOne(id);
        return ResponseUtil.wrapOrNotFound(roomGoodSearchDTO);
    }

    /**
     * {@code DELETE  /room-good-searches/:id} : delete the "id" roomGoodSearch.
     *
     * @param id the id of the roomGoodSearchDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/room-good-searches/{id}")
    public ResponseEntity<Void> deleteRoomGoodSearch(@PathVariable Long id) {
        log.debug("REST request to delete RoomGoodSearch : {}", id);
        roomGoodSearchService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
            .build();
    }
}
